<?php

namespace app\xwam\controller;

/**
 * ProductInvoicing 控制器类
 *
 * @since 1.0 <2016-5-28> SoChishun <14507247@qq.com> Added.
 */
class ProductInvoicingController extends AppbaseController {

    function indexSearch() {
        $search = I('param.');
        $where = array();
        if (!empty($search['word'])) {
            $where['g.title'] = array('like', '%' . $search['word'] . '%');
        }
        if (!empty($search['category_id'])) {
            $where['g.category_id'] = $search['category_id'];
        }
        if (!empty($search['product_id'])) {
            $where['i.product_id'] = $search['product_id'];
        }
        $where['i.site_id'] = $this->siteId;
        return array('search' => $search, 'where' => $where);
    }

    function index($product_id = 0) {
        $asearch = $this->indexSearch();
        $mInvoicing = new \app\xwam\model\ProductInvoicingModel();
        $list = $mInvoicing->selectPaging($page, array('table' => 't_porg_product_invoicing i left join t_porg_product_goods g on i.product_id=g.id', 'where' => $asearch['where'], 'field' => 'i.*, g.title as product_title', 'order' => 'i.id desc'), array('page_params' => $asearch['search']));
        $this->assign('list', $list);
        $this->assign('page', $page->showBootstrap());
        $this->assign('search', $asearch['search']);
        return view();
    }

    function edit($product_id = 0) {
        if (!$product_id) {
            exit('参数有误!');
        }
        $mProduct=new \app\xwam\model\ProductGoodsModel();
        $data=$mProduct->find($product_id);
        $this->assign('data', $data);
        return view();
    }

    function editSave() {
        $mInvoicing = new \app\xwam\model\ProductInvoicingModel();
        $result = $mInvoicing->saveInvoicing();
        $this->dialogJump($result['status'], $result['info']);
    }

    // 编辑备注 2016-6-7
    function remark_edit($id = 0) {
        if (!$id) {
            $this->dialogClose(false, array('error' => '参数有误!'));
        }
        $mInvoicing = new \app\xwam\model\ProductInvoicingModel();
        $this->assign('data', $mInvoicing->find($id));
        return view();
    }

    function remark_edit_save($id = 0, $remark = '') {
        if (!$id) {
            $this->error('参数有误!');
        }
        $mInvoicing = new \app\xwam\model\ProductInvoicingModel();
        $mInvoicing->where(array('id' => $id))->setField('remark', $remark);
        $this->dialogJump(true);
    }

    function changeStatus($id, $status) {
        $mInvoicing = new \app\xwam\model\ProductInvoicing();
        $this->ajaxReturn($mInvoicing->changeStatus($id, $status));
    }

    function changeSort($id, $sort) {
        $mInvoicing = new \app\xwam\model\ProductInvoicing();
        $this->ajaxReturn($mInvoicing->changeSort($id, $sort));
    }

    function delete($id = '') {
        $mInvoicing = new \app\xwam\model\ProductInvoicing();
        $result = $mInvoicing->deleteInvoicing($id);
        $this->dialogJump($result['status'], $result['info']);
    }
    // 2017-3-22
    function getOperateTypeJson($direct=''){
        $data= \app\xwam\model\ProductInvoicingModel::getOperateType($direct);
        $this->ajaxReturn($data);
    }
    // 2017-3-22
    function getDirectionType(){
        $data= \app\xwam\model\ProductInvoicingModel::getDirectionType();
        $this->ajaxReturn($data);
    }
}
